/**
 * 协同H5表单自定义控件js
 * @author wxju
 * @since V5-A8 7.0
 */


var _room_storge_key = "m3_v5_collaboration_newCol_room_cache_key_";

/**
 * 初始化表单自定义控件
 */
function _customcontrol_meetingroom_init(e) {
	var fieldMessage = e.detail.data.model;
	var fieldInfoMessage = e.detail.data.fieldInfo;
	
	if (fieldMessage!=null && fieldMessage.fieldName!=null) {
		if (null != e.detail.data.model.extMap.extendParam) {
			var extendParam = JSON.parse(e.detail.data.model.extMap.extendParam);
			var str = 'customcontrol_' + extendParam.key;
			if (typeof window[str] === 'function') {
				window[str].call(this, e);
			}
		}
	}
}
//会议室选择自定义控件实现方法
function customcontrol_35000(param) {
	if (param.detail.data.model.auth === 'browse') {
		//如果是浏览态就不做组件的替换了，直接返回，当然客户也可以根据需要自己替换html代码片段，实现浏览态时的效果
		return;
	}

	//根据传进来的组件类型以及字段的表名，字段名，记录id等生成组件唯一的标识符
	var uuid = 'customcontrol_35000_'
			+ param.detail.data.fieldInfo.ownerTableName + '_'
			+ param.detail.data.recordId + '_'
			+ param.detail.data.fieldInfo.name;
	//根据标识符查询页面中是否已经渲染了自定义组件
	var myComponent = document.getElementById(uuid);

	//如果当前组件已经被初始化，则直接更新数据，否则
	if (myComponent) {
		myComponent.value = param.detail.data.model.value;
	} else {
		//初始化组件，替换表单中对应的dom
		var display = param.detail.data.model.display;
		var value = param.detail.data.model.value;
		var cacheKey = _room_storge_key + param.detail.data.fieldInfo.name;//缓存的Key值
		var cacheValue = JSON.parse(cmp.storage.get(cacheKey, true));
		//清空会议室缓存
		cmp.storage["delete"](cacheKey, true);
		
		if (cacheValue!=null) {
			display = cacheValue.display;
			value = cacheValue.value;
		}
		
		var html = '<div id="'+uuid+'" class="sui-form-ctrl-value" style="width: 100%;" />'+ display + '</div>'
		html += '<i id="right_'+uuid+'" class="see-icon-v5-form-pull-right" style="display:none"></i>';
		html += '<i id="clear_'+ uuid +'" class="see-icon-v5-form-close-circle-fill" style="display:none"></i>';
		
		param.detail.data.target.innerHTML = html;
		
		//向表单数据中赋值（保存的值）
	    param.detail.data.handler.set(value);
	    param.detail.data.model.display = display;
	    
		var rightButton = document.getElementById("right_"+uuid);
		var clearButton = document.getElementById("clear_"+uuid);
		var roomInput = document.getElementById(uuid);
		
		if (display == "") {
			rightButton.style.display = "";
		} else {//显示叉
			clearButton.style.display = "";
		}

		roomInput.addEventListener('tap', function() {
			_goToChooseMeetingRoom(param,uuid,cacheKey);
		});
		rightButton.addEventListener('tap', function() {
			_goToChooseMeetingRoom(param,uuid,cacheKey);
		});
		clearButton.addEventListener('tap', function() {
			_clearRoomValue(param,uuid);
		});

	}
}

function _goToChooseMeetingRoom(param,uuid,cacheKey) {
	var params = {
			formChooseKey : cacheKey,
			action : "formChoose"
	};
	cmp.event.trigger("beforepageredirect",document);

    cmp.href.next(_meetingPath + "/html/meetingRoomList.html" + colBuildVersion, params);
}

function _clearRoomValue(param,uuid) {
	var str = '';
	
	document.getElementById(uuid).innerHTML = str;
	
	param.detail.data.handler.set(str);
	param.detail.data.model.display = "";
	
	document.getElementById("clear_"+uuid).style.display = "none";
	document.getElementById("right_"+uuid).style.display = "";
}